package cn.dengta.webapp.live.entity;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Nullable;

import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Cmp;
import me.codeplayer.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 直播PK记录
 *
 * @author xy
 * @since 2023-01-10
 */
@Setter
@Getter
@TableName("im_live_pk_log")
public class LivePkLog extends TimeBasedEntity {

	private static final long serialVersionUID = 1L;
	/** 关闭PK */
	public static final int CLOSED = 4;
	/** 中断结束 */
	public static final int INTERRUPT = 3;
	/** 结束 */
	public static final int END = 2;
	/** PK中 */
	public static final int PK = 1;
	/** -1=拒绝； */
	public static final int CANCEL = -1;
	/** 未接 */
	public static final int NO_ACCEPT = -2;

	/** 直播场次 */
	Long liveId;
	/** PK 接受人直播标识 */
	Long toLiveId;
	/** PK发起人 */
	Long userId;
	/** PK接收人 */
	Long toUserId;
	/** 1=好友；2=推荐；3=随机 */
	Integer type;
	/** PK 胜方 用户id */
	Long winUserId;
	/** 邀请方PK总的钻石数 */
	Integer coins;
	/** 被邀请方PK总的钻石数 */
	Integer toCoins;
	/** 前3甲用户逗号拼接，格式：$userId_$coin,$userId1_$coin */
	String top3;
	/** 对方前3甲用户逗号拼接，格式：$userId_$coin,$userId1_$coin */
	String toTop3;
	/** 邀请方PK助力人数 */
	Integer total;
	/** 被邀请方PK助力人数 */
	Integer toTotal;
	/** PK开始时间 */
	Date beginTime;
	/** PK结束时间 */
	Date endTime;
	/** 直播关闭时间 */
	Date closeTime;
	/** -2=未接; -1=拒绝；0=初始化；1=PK中；2=PK结束；3=PK关闭 */
	Integer status;
	/** 混流任务发起时的PK记录ID */
	Long taskLogId;
	/** PK次数 */
	transient Integer liveCount;

	public static LivePkLog of(Long liveId, Long toLiveId, Long userId, Long toUserId, Integer type) {
		LivePkLog log = new LivePkLog();
		log.setLiveId(liveId);
		log.setToLiveId(toLiveId);
		log.setUserId(userId);
		log.setToUserId(toUserId);
		log.setType(type);
		log.setStatus(0); // 0=初始化；
		return log;
	}

	/** 剩余PK时长 单位秒 */
	public Long buildDuration() {
		if (this.getEndTime().getTime() > System.currentTimeMillis()) {
			return (this.getEndTime().getTime() - System.currentTimeMillis()) / 1000;
		} else {
			return 0L;
		}
	}

	/** PK关闭时长 单位秒 */
	public Long buildCloseDuration() {
		if (this.hasEnd()) {
			return (System.currentTimeMillis() - this.getEndTime().getTime()) / 1000;
		} else {
			return 0L;
		}
	}

	/** 判断两个用户是否是当前PK记录的主播 */
	public boolean hasOwnAnchor(Long userId, Long toUserId) {
		return (this.userId.equals(userId) || this.toUserId.equals(userId)) && (this.userId.equals(toUserId) || this.toUserId.equals(toUserId));
	}

	/** 是否可接受邀请 */
	public boolean canAccept() {
		return Cmp.eq(this.status, 0);
	}

	/** 是否是初始化 */
	public boolean hasInit() {
		return Cmp.eq(this.status, 0);
	}

	/** 是否在PK中 */
	public boolean hasPk() {
		return Cmp.eq(this.status, PK);
	}

	/** 是否结束 */
	public boolean hasEnd() {
		return Cmp.eq(this.status, END) || Cmp.eq(this.status, INTERRUPT);
	}

	/** PK是否已关闭 */
	public boolean hasClosed() {
		return Cmp.eq(this.status, CLOSED);
	}

	/** 是否已取消 */
	public boolean hasCancel() {
		return Cmp.eq(this.status, CANCEL) || Cmp.eq(this.status, NO_ACCEPT);
	}

	public Long sendLiveId(@Nullable Long userId) {
		return Objects.equals(userId, toUserId) ? toLiveId : liveId;
	}

	public Long sendUserId(@Nullable Long userId) {
		return Objects.equals(userId, toUserId) ? toUserId : userId;
	}

	public Integer sendCoins(@Nullable Long userId) {
		return Objects.equals(userId, toUserId) ? toCoins : coins;
	}

	public Integer sendTotal(@Nullable Long userId) {
		return Objects.equals(userId, toUserId) ? toTotal : total;
	}

	public String pkResult_(@Nullable Long userId) {
		return winUserId != null ? Objects.equals(userId, winUserId) ? "胜" : "负" : "平";
	}

	public String fromChannelName() {
		return Live.channelName(userId, liveId);
	}

	public String toChannelName() {
		return Live.channelName(toUserId, toLiveId);
	}

	@Nullable
	public Long getMvpUserId() {
		if (winUserId != null || coins.intValue() != toCoins) {
			final boolean fromWin = winUserId != null ? winUserId.equals(userId) : coins > toCoins;
			String top3 = fromWin ? this.top3 : this.toTop3;
			if (StringUtil.notEmpty(top3)) {
				return resolveMvpUserId(top3);
			}
		}
		return null;
	}

	public static Long resolveMvpUserId(String top3) {
		return Long.valueOf(StringUtils.substringBefore(top3, '_'));
	}

	public static final String LIVE_ID = "live_id";
	public static final String TO_LIVE_ID = "to_live_id";
	public static final String USER_ID = "user_id";
	public static final String TO_USER_ID = "to_user_id";
	public static final String TYPE = "type";
	public static final String COINS = "coins";
	public static final String WIN_USER_ID = "win_user_id";
	public static final String TO_COINS = "to_coins";
	public static final String TOP3 = "top3";
	public static final String TO_TOP3 = "to_top3";
	public static final String TOTAL = "total";
	public static final String TO_TOTAL = "to_total";
	public static final String BEGIN_TIME = "begin_time";
	public static final String END_TIME = "end_time";
	public static final String STATUS = "status";

}
